Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = exports.toOpenSetting = exports.openSettingModal = exports.loadApi = exports.getScope = void 0;

var _util = _interopRequireDefault(require("./util"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _objectSpread(t) {
    for (var e = 1; e < arguments.length; e++) {
        var o = null != arguments[e] ? arguments[e] : {}, n = Object.keys(o);
        "function" == typeof Object.getOwnPropertySymbols && (n = n.concat(Object.getOwnPropertySymbols(o).filter(function(e) {
            return Object.getOwnPropertyDescriptor(o, e).enumerable;
        }))), n.forEach(function(e) {
            _defineProperty(t, e, o[e]);
        });
    }
    return t;
}

function _defineProperty(e, t, o) {
    return t in e ? Object.defineProperty(e, t, {
        value: o,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[t] = o, e;
}

var scopeList = [ {
    desc: "地理位置",
    scope: "userLocation",
    api: [ "getLocation", "chooseLocation", "openLocation" ]
}, {
    desc: "通讯地址",
    scope: "address",
    api: [ "chooseAddress" ]
}, {
    desc: "发票抬头",
    scope: "invoiceTitle",
    api: [ "chooseInvoiceTitle" ]
}, {
    desc: "微信运动步数",
    scope: "werun",
    api: [ "getWeRunData" ]
}, {
    desc: "录音功能",
    scope: "record",
    api: [ "startRecord" ]
}, {
    desc: "保存到相册",
    scope: "writePhotosAlbum",
    api: [ "saveImageToPhotosAlbum", "saveVideoToPhotosAlbum" ]
}, {
    desc: "摄像头",
    scope: "camera",
    api: [ "camera" ]
} ], getScope = function(t) {
    return scopeList.find(function(e) {
        return e.api.includes(t);
    });
};

exports.getScope = getScope;

var compatibility = function(e, t) {
    wx.hideLoading(), openSettingModal(e, t);
}, loadApi = function(t, o, e) {
    (void 0 === o.loading || o.loading) && wx.showLoading(), (e ? wx.sleep(.5) : Promise.resolve()).then(function() {
        wx[t](_objectSpread({}, o, {
            fail: function(e) {
                handleFail(t, e), o.fail && o.fail();
            },
            complete: function() {
                wx.hideLoading(), o.complete && o.complete();
            }
        }));
    });
};

exports.loadApi = loadApi;

var openSettingModal = function(e, t) {
    (getCurrentPages().slice(-1)[0].selectComponent("#xmpAuthModal") || {
        show: function() {}
    }).show(e, t);
};

exports.openSettingModal = openSettingModal;

var toOpenSetting = function(t) {
    return wx.fPromise("openSetting").then(function(e) {
        return console.log(e), e.authSetting && e.authSetting["scope.".concat(getScope(t).scope)] ? Promise.resolve() : Promise.reject();
    });
};

exports.toOpenSetting = toOpenSetting;

var handleFail = function(e, t) {
    switch (e) {
      case "startRecord":
        /short/.test(t.errMsg) ? wx.showToast({
            title: "录音时间太短",
            image: "/images/warning.png"
        }) : _util.default.qAlert(t.errMsg, "获取".concat(getScope(e).desc, "失败"));
    }
}, _default = {
    facility: function(e) {
        var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {};
        wx.authorize({
            scope: "scope.".concat(getScope(e).scope),
            success: function() {
                return loadApi(e, t, !1);
            },
            fail: function() {
                return compatibility(e, t);
            },
            complete: function(e) {
                console.log("authorize", e);
            }
        });
    }
};

exports.default = _default;